﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Data;
using System.Reflection;
using System.Threading;

namespace UserReader
{
    class Program
    {
        static void Main(string[] args)
        {

            Thread t1 = new Thread(new ThreadStart(DoE));

            Thread t2 = new Thread(new ThreadStart(DoE));
            Console.ReadLine();
            t1.Start();
            t2.Start();

            Console.ReadLine();
            Thread t3 = new Thread(new ThreadStart(DoE));
            Thread t4 = new Thread(new ThreadStart(DoE));

            t3.Start();
            t4.Start();

            while (Console.ReadLine() == "p")
            {

                //foreach (dynamic item in infos)
                //{
                //    Console.Write("{0}--{1}--{2}\r\n", item.Id, item.Name, AppDomain.CurrentDomain.GetAssemblies().Count());
                //}

                foreach (Assembly item in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (!item.IsDynamic)
                    {
                        continue;
                    }
                    Console.Write("FullName：{0}\r\n", item.FullName);

                }
            }


        }

        public static void DoE()
        {
            DataTable dt = new DataTable("tmp");
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            //dt.Columns.Add("AppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetA"
            //    + "AppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetA");
            for (int index = 0; index < 2; index++)
            {
                DataRow dr = dt.NewRow();
                dr["Id"] = index.ToString();
                dr["Name"] = "Name" + index.ToString();
                dt.Rows.Add(dr);
            }
            DataTable dt1 = new DataTable("tmp");
            dt1.Columns.Add("Id");
            dt1.Columns.Add("Name");
            dt1.Columns.Add("Name2");
            //dt1.Columns.Add("1AppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetA"
            //    + "AppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetAssembliesAppDomainCurrentDomainGetA");
            for (int index = 0; index < 2; index++)
            {
                DataRow dr = dt1.NewRow();
                dr["Id"] = index.ToString();
                dr["Name"] = "Name" + index.ToString();
                dt1.Rows.Add(dr);
            }
            for (int index = 0; index < 10; index++)
            {
                List<dynamic> infos = GetList(dt);
                List<dynamic> infos2 = GetList(dt1);
            }

        }

        public static List<Object> GetList(DataTable dt)
        {
            List<object> objList = new List<object>();
            if (dt == null || dt.Rows.Count == 0)
            {
                return objList;
            }

            List<string> props = new List<string>();

            foreach (DataColumn item in dt.Columns)

            {
                props.Add(item.ColumnName);
            }

            CustomType customType = new CustomType();
            Type type = customType.CreateType2(props);

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (DataColumn item in dt.Columns)
                {
                    dict.Add(item.ColumnName, dr[item.ColumnName].ToString());
                }
                Object result = customType.GetInstance(type, dict);
                objList.Add(result);
            }
            //GC.Collect();
            return objList;
        }

        private static void NewMethod()
        {

            ADHelper helper = new ADHelper("LDAP://founder", "founder", "zyadmin", "Founder@2012");
            //  DirectoryEntry de=  helper.GetDirectoryEntryByAccount("zhangwei21312");
            List<DirectoryEntry> ls = helper.GetListByAccount("zhangwei");
            Console.WriteLine("共 {0} 项", ls.Count);
            foreach (DirectoryEntry item in ls)
            {
                // Console.WriteLine("共 {0} 项", item.Properties.PropertyNames.Count);
                Console.WriteLine("{0}:{1}", item.Properties["displayName"].Value, item.Properties["sAMAccountName"].Value);
                //foreach (string propertyName in item.Properties.PropertyNames)
                //{
                //    Console.WriteLine("{0}:{1}", propertyName, item.Properties[propertyName].Value);
                //}
            }
        }
        //#region## 是否连接到域
        ///// <summary>
        ///// 功能：是否连接到域
        ///// 作者：Wilson
        ///// 时间：2012-12-15
        ///// http://msdn.microsoft.com/zh-cn/library/system.directoryservices.directoryentry.path(v=vs.90).aspx
        ///// </summary>
        ///// <param name="domainName">域名或IP</param>
        ///// <param name="userName">用户名</param>
        ///// <param name="userPwd">密码</param>
        ///// <param name="entry">域</param>
        ///// <returns></returns>
        //private bool IsConnected(string domainName, string userName, string userPwd, out DirectoryEntry domain)
        //{
        //    domain = new DirectoryEntry();
        //    try
        //    {
        //        domain.Path = string.Format("LDAP://{0}", domainName);
        //        domain.Username = userName;
        //        domain.Password = userPwd;
        //        domain.AuthenticationType = AuthenticationTypes.Secure;

        //        domain.RefreshCache();

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {

        //        return false;
        //    }
        //}
        //#endregion
    }
}
